#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "equ_include/QY_BMS_Data.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    canset=new CANset();
    Interface_Rtimer=new QTimer(this);

    Interface_Init();
    Equ_runinfo_init();
    Charts_Init();
    FaultListModel= new QStringListModel(this);
    FaultListModel->setStringList(FaultList);
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Data_Refresh()));
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Charts_Show()));
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(HidFault_Scan()));
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Fault_show()));
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Interface_Refresh()));


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::Interface_Init()
{

    Interface_rt=500;
    Interface_Rtimer->stop();
    Interface_Rtimer->setInterval(Interface_rt);//毫秒
    Interface_Rtimer->start();
    for(int i=0;i<18;i++)
    {
        BMS_data[i]=0;
        V_max[i]=0;
        V_min[i]=0;
    }
    Box_V_max=0;
    Box_V_min=0;
    Box_V_max_Num=0;
    Box_V_min_Num=0;
    axisY_val_max=0;
    axisY_val_min=0;
    git_new_titlebar=0;
    Save_Data=0;
    QY_BMS_Data::rest_data_info();




}

void MainWindow::Equ_runinfo_init()
{
    ui->tableView_data->setModel(&QY_BMS_Data_mode_info);
    ui->tableView->setModel(&CVM_main_info);
}

void MainWindow::Hid_fault_Init()
{



}


void MainWindow::Charts_Init()
{
    QStringList categories;
    for (int i=1;i<19;i++)
    {
        QString buff=QString::number(i);
        categories.append(buff);    // 将buff中的数据加到列表中

    }
    QFont font("Arial",4);
    chart=new QChart;
    chart->setTitle("BMS电压图");
    set0->setColor(Qt::green);
    set_max->setColor(Qt::red);
    set_min->setColor(Qt::blue);
    chart->setAnimationOptions(QChart::NoAnimation);
    //series->append(set0);
    series->setBarWidth(0.9);        //柱状图宽度
    series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);  //设置标签显示的位置
    series->setLabelsVisible(true);  //设置数据标签可见
    chart->addSeries(series);        //X轴标识
    axisX=new QBarCategoryAxis;
    axisY=new QValueAxis;
    axisX->setLabelsFont(font);
    axisX->append(categories);
    axisY->setRange(-5000,5000);          //Y轴的范围
    axisY->setTickCount(11);              //Y分成多少格
    chart->createDefaultAxes();
    chart->addAxis(axisX, Qt::AlignBottom); //添加坐标轴X
    chart->addAxis(axisY, Qt::AlignRight);   //添加坐标轴Y
    series->attachAxis(axisX);
    series->attachAxis(axisY);
    ui->chartView->setChart(chart);
    ui->chartView->setRenderHint(QPainter::Antialiasing);
    ui->chartView->setRubberBand(QChartView::VerticalRubberBand);


}

int MainWindow::randomSignedDouble(int min, int max ) {
    int randomInt = QRandomGenerator::global()->bounded(min, max + 1);;
    return randomInt;
}


void MainWindow::Data_Refresh()
{


    QY_BMS_Data::CAN_loss_rest(3);

/*
    QY_BMS_Data_info.V_F1=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F2=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F3=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F4=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F5=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F6=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F7=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F8=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F9=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F10=randomSignedDouble(-100,3000);
    QY_BMS_Data_info.V_F11=randomSignedDouble(-100,3000);
    QY_BMS_Data_info.V_F12=randomSignedDouble(-100,3000);
    QY_BMS_Data_info.V_F13=randomSignedDouble(-100,3000);
    QY_BMS_Data_info.V_F14=randomSignedDouble(-100,3000);
    QY_BMS_Data_info.V_F15=randomSignedDouble(-100,3000);
    QY_BMS_Data_info.V_F16=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F17=randomSignedDouble(-100,5000);
    QY_BMS_Data_info.V_F18=randomSignedDouble(-100,5000);
*/
    BMS_data[0]=QY_BMS_Data_info.V_F1;
    BMS_data[1]=QY_BMS_Data_info.V_F2;
    BMS_data[2]=QY_BMS_Data_info.V_F3;
    BMS_data[3]=QY_BMS_Data_info.V_F4;
    BMS_data[4]=QY_BMS_Data_info.V_F5;
    BMS_data[5]=QY_BMS_Data_info.V_F6;
    BMS_data[6]=QY_BMS_Data_info.V_F7;
    BMS_data[7]=QY_BMS_Data_info.V_F8;
    BMS_data[8]=QY_BMS_Data_info.V_F9;
    BMS_data[9]=QY_BMS_Data_info.V_F10;
    BMS_data[10]=QY_BMS_Data_info.V_F11;
    BMS_data[11]=QY_BMS_Data_info.V_F12;
    BMS_data[12]=QY_BMS_Data_info.V_F13;
    BMS_data[13]=QY_BMS_Data_info.V_F14;
    BMS_data[14]=QY_BMS_Data_info.V_F15;
    BMS_data[15]=QY_BMS_Data_info.V_F16;
    BMS_data[16]=QY_BMS_Data_info.V_F17;
    BMS_data[17]=QY_BMS_Data_info.V_F18;
    QY_BMS_Data::EQ_runinfo();
    runinfo();
    ui->tableView_data->viewport()->update();
    ui->tableView->viewport()->update();
    qDebug()<<"QY_BMS_Data_info.V_F10="<<QY_BMS_Data_info.V_F10;
}


void MainWindow::Protect_fun()
{



}


void MainWindow::HidFault_Scan()
{

QY_BMS_Data::Fault_processing();

}


void MainWindow::Charts_Show()
{
    set0->remove(0,18);
    set_max->remove(0,18);
    set_min->remove(0,18);
    Box_V_max=BMS_data[0];
    Box_V_min=BMS_data[0];

    for (int i=0;i<18;i++)
    {
        V_max[i]=0;
        V_min[i]=0;
        if(BMS_data[i]>Box_V_max)
        {
            Box_V_max=BMS_data[i];
            Box_V_max_Num=i;
        }
        if(BMS_data[i]<Box_V_min)
        {
            Box_V_min=BMS_data[i];
            Box_V_min_Num=i;
        }
    }
    V_max[Box_V_max_Num]=Box_V_max;
    V_min[Box_V_min_Num]=Box_V_min;
    BMS_data[Box_V_max_Num]=0;
    BMS_data[Box_V_min_Num]=0;



    int Y_k=Box_V_max/1000;
    axisY_val_max=(Y_k+1)*1000;
    if(Box_V_min<0)
    {
        axisY_val_min=int(Box_V_min)-10;
    }
    else
    {
        axisY_val_min=0;
    }

    axisY->setRange(axisY_val_min,axisY_val_max);

    for (int i=0;i<18;i++)
    {
        *set_max<<V_max[i];
        *set_min<<V_min[i];
        *set0<<BMS_data[i];
    }
    //series->clear();
    series->insert(0,set_max);
    series->insert(0,set_min);
    series->insert(0,set0);
    ui->chartView->setChart(chart);
    qDebug()<<"图表刷新";
}


void MainWindow::Interface_Refresh()
{

emit Displayrefresh_signal(1);
save_data();
}


void MainWindow::Fault_show()
{

    FaultList.append(QY_BMS_Data_info.FaultList);
    //FaultList.append(TPSensorTestingBench_info_1.FaultList);
    FaultListModel->setStringList(FaultList);
    ui->listView_fault_list->viewport()->update();//故障列表刷新
    FaultList.clear();

}


void MainWindow::EQRun_loglist()
{



}


void MainWindow::save_data()
{

    if(Save_Data==1)
    {
        if(git_new_titlebar==0)
        {
            ADD_Savedata_Titlebar();
            git_new_titlebar=1;
        }
        else
        {
            ADD_Savedata_Line();
        }

    }

}


void MainWindow::Auto_Runlog_info()
{



}

void MainWindow::ADD_Savedata_Titlebar()
{
    QFile savedata_file(fileName);
    savedata_file.open(QIODevice::Append);
    QTextStream csvOutPut(&savedata_file);
    QString str_titlebar;
    str_titlebar="时间,";
    for (int i = 0; i < QY_BMS_Data_mode_info.rowCount(); i++)
    {
        QString cell=QY_BMS_Data_mode_info.index(i, 0).data().toString()+",";
        str_titlebar=str_titlebar+cell;
    }
    csvOutPut<<str_titlebar;
    //csvOutPut<<"\n"<<str_titlebar;
    savedata_file.close();
    //qDebug()<<"写入标题完成";


}

void MainWindow::ADD_Savedata_Line()
{

    QFile savedata_file(fileName);
    QDateTime  s_t=QDateTime::currentDateTime();
    QString systime =s_t.toString("yyyy-MM-dd HH:mm:ss");
    savedata_file.open(QIODevice::Append);
    QTextStream csvOutPut(&savedata_file);
    QString str_line;
    str_line=systime+",";

    for (int i = 0; i < QY_BMS_Data_mode_info.rowCount(); i++)
    {
        QString cell=QY_BMS_Data_mode_info.index(i, 1).data().toString()+",";
        str_line=str_line+cell;
    }
    csvOutPut<<"\n"<<str_line;
    savedata_file.close();
    //qDebug()<<"写入数据完成";

}


void MainWindow::on_actionCAN_triggered()
{
    canset->show();
}

void MainWindow::runinfo()
{
    CVM_main_info.setHorizontalHeaderLabels(QStringList()<<QStringLiteral("项目")<<QStringLiteral("数值")<<QStringLiteral("单位"));
    CVM_main_info.setItem(0,0,new QStandardItem("总电流"));
    CVM_main_info.setItem(0,1,new QStandardItem(QString::number(QY_BMS_Data_info.BMS_A)));
    CVM_main_info.setItem(0,2,new QStandardItem("A"));
    CVM_main_info.setItem(1,0,new QStandardItem("总电压"));
    CVM_main_info.setItem(1,1,new QStandardItem(QString::number(QY_BMS_Data_info.V_SUM)));
    CVM_main_info.setItem(1,2,new QStandardItem("V"));
    CVM_main_info.setItem(2,0,new QStandardItem("最大值"));
    CVM_main_info.setItem(2,1,new QStandardItem(QString::number(QY_BMS_Data_info.V_MAX)));
    CVM_main_info.setItem(2,2,new QStandardItem("mV"));
    CVM_main_info.setItem(3,0,new QStandardItem("最小值"));
    CVM_main_info.setItem(3,1,new QStandardItem(QString::number(QY_BMS_Data_info.V_MIN)));
    CVM_main_info.setItem(3,2,new QStandardItem("mV"));
    CVM_main_info.setItem(4,0,new QStandardItem("平均值"));
    CVM_main_info.setItem(4,1,new QStandardItem(QString::number(QY_BMS_Data_info.V_AVG)));
    CVM_main_info.setItem(4,2,new QStandardItem("mV"));
    CVM_main_info.setItem(5,0,new QStandardItem("方差"));
    CVM_main_info.setItem(5,1,new QStandardItem(QString::number(QY_BMS_Data_info.V_VAR)));
    CVM_main_info.setItem(5,2,new QStandardItem("mV²"));
    CVM_main_info.setItem(6,0,new QStandardItem("模拟采集标准电压"));
    CVM_main_info.setItem(6,1,new QStandardItem(QString::number(QY_BMS_Data_info.AD_V)));
    CVM_main_info.setItem(6,2,new QStandardItem("mV"));
    CVM_main_info.setItem(7,0,new QStandardItem("正电源电压"));
    CVM_main_info.setItem(7,1,new QStandardItem(QString::number(QY_BMS_Data_info.Power_V1)));
    CVM_main_info.setItem(7,2,new QStandardItem("V"));
    CVM_main_info.setItem(8,0,new QStandardItem("PCB温度"));
    CVM_main_info.setItem(8,1,new QStandardItem(QString::number(QY_BMS_Data_info.PCB_TEMP)));
    CVM_main_info.setItem(8,2,new QStandardItem("℃"));
    CVM_main_info.setItem(9,0,new QStandardItem("有效通道数"));
    CVM_main_info.setItem(9,1,new QStandardItem(QString::number(QY_BMS_Data_info.Channel_NUB)));
    CVM_main_info.setItem(9,2,new QStandardItem(""));
    CVM_main_info.setItem(10,0,new QStandardItem("OC使能位"));
    CVM_main_info.setItem(10,1,new QStandardItem(QString::number(QY_BMS_Data_info.OC_EN)));
    CVM_main_info.setItem(10,2,new QStandardItem(""));
    CVM_main_info.setItem(11,0,new QStandardItem("OC选择位"));
    CVM_main_info.setItem(11,1,new QStandardItem(QString::number(QY_BMS_Data_info.DO_OC)));
    CVM_main_info.setItem(11,2,new QStandardItem(""));
    CVM_main_info.setItem(12,0,new QStandardItem("温度1"));
    CVM_main_info.setItem(12,1,new QStandardItem(QString::number(QY_BMS_Data_info.TEMP1)));
    CVM_main_info.setItem(12,2,new QStandardItem("℃"));
    CVM_main_info.setItem(13,0,new QStandardItem("温度2"));
    CVM_main_info.setItem(13,1,new QStandardItem(QString::number(QY_BMS_Data_info.TEMP2)));
    CVM_main_info.setItem(13,2,new QStandardItem("℃"));

}

void MainWindow::on_pushButton_clicked()
{
    QY_BMS_Data_info.FaultList.clear();
}


void MainWindow::on_pushButton_SaveData_clicked()
{
    if(fileName.isEmpty())
    {
        QMessageBox::information(this, "警告", "请先设置文件路径!");
        //qDebug()<<fileName.isEmpty();
    }
    else
    {
        if(ui->pushButton_SaveData->text()=="开启记录")
        {
            QFile savedata_file(fileName);
            if(!savedata_file.open(QIODevice::WriteOnly | QIODevice::Append))
            {
                QMessageBox::information(this, "警告", "文件被打开!");
                //qDebug()<<"文件被打开";
            }
            else
            {
                Save_Data=1;
                ui->pushButton_SaveData->setText("暂停记录");
            }

        }
        else
        {
            Save_Data=0;
            ui->pushButton_SaveData->setText("开启记录");
        }
    }
}


void MainWindow::on_pushButton_SaveData_set_clicked()
{
    fileName = QFileDialog::getSaveFileName(this,QStringLiteral("打开文件"),".",tr("csv files(*.csv)"));
    git_new_titlebar=0;
}

